package cn.springcloud.fix.demo.reflex;

import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

public class ClassTest {

    public static void main(String[] args) throws IllegalAccessException, InstantiationException, ClassNotFoundException {
        //方式1:
        Class c1 = User.class;

        //方式2:
        User u = new User();
        Class c2 = u.getClass();

        //方式3:
        Class c3 = Class.forName("cn.springcloud.fix.demo.reflex.User");

        //可以通过类的类型创建该类的实例对象
        User user = (User) c1.newInstance();

        Class c4 = String.class;
        Class c5 = int.class;
        Class c6 = void.class;
        //System.out.println(c1.getName());
        //System.out.println(c6.getName());
        //
        //System.out.println(c2.getSimpleName());
        //System.out.println(c6.getSimpleName());

        String s = "ss";
        //printClassInfo(s);
        //printFiledInfo(s);
        //printConstructInfo(s);

        add();
    }

    private static void add() {
        User a = new User();
        Class c = a.getClass();
        try {
            Method method = c.getMethod("add", int.class, int.class);
            method.invoke(a, 10, 10);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void printClassInfo(Object object) {
        Class c = object.getClass();
        System.out.println("类的名称：" + c.getName());

        /**
         * 一个成员方法就是一个method对象
         * getMethod()所有的 public方法，包括父类继承的 public
         * getDeclaredMethods()获取该类所有的方法，包括private ,但不包括继承的方法。
         */
        Method[] methods = c.getMethods();//获取方法

        //获取所有的方法，包括private ,c.getDeclaredMethods();
        for (Method method : methods) {
            //得到方法的返回类型
            Class returnType = method.getReturnType();
            System.out.print(returnType.getName());
            //得到方法名：
            System.out.print(method.getName() + "(");
            Class[] parameterTypes = method.getParameterTypes();
            for (Class class1 : parameterTypes) {
                System.out.print(class1.getName() + ",");
            }
            System.out.println(")");
        }
    }

    /**
     * getFileds()获取public
     * getDeclaredFields()获取所有
     */
    private static void printFiledInfo(Object o) {
        Class c = o.getClass();
        Field[] fileds = c.getDeclaredFields();
        for (Field f : fileds) {
            //获取成员变量的类型
            Class filedType = f.getType();
            System.out.println(filedType.getName() + " " + f.getName());
        }
    }

    public static void printConstructInfo(Object o) {
        Class c = o.getClass();
        Constructor[] constructors = c.getDeclaredConstructors();
        for (Constructor con : constructors) {
            System.out.print(con.getName() + "(");
            Class[] typeParas = con.getParameterTypes();
            for (Class class1 : typeParas) {
                System.out.print(class1.getName() + " ,");
            }
            System.out.println(")");
        }
    }
}
